import * as THREE from 'three'
import { KongZhi } from './KongZhi.js'
import { sheZhi } from '../cunChu/bianLiang.js'
import { wanJiaKongZhi } from './WanJiaKongZhi.js'
import { ziYouShiJiao } from './ZiYouShiJiao.js'
import { zhiZaoMoShi } from './ZhiZaoMoShi.js'
import { bianJiMoShi } from './BianJiMoShi.js'
import { jieMian } from '../jieMian/zuJian/JieMian.js'

// 全局的默认快捷键
let moRenKuaiJieJian = {
	wanJiaKongZhi: {
		miaoShu: '玩家控制',
		kuaiJieJian: 'F9'
	},
	ziYouShiJiao: {
		miaoShu: '自由视角',
		kuaiJieJian: 'F10'
	},
	zhiZaoMoShi: {
		miaoShu: '制造模式',
		kuaiJieJian: 'KeyZ'
	},
	bianJiMoShi: {
		miaoShu: '编辑模式',
		kuaiJieJian: 'KeyB'
	}
}

Object.defineProperty(moRenKuaiJieJian, 'miaoShu', {
	value: '键盘事件',
	enumerable: false
})

export class JianPanShiJian extends KongZhi {
	kuaiJieJianBianHao = 'JianPanShiJian'
	kuaiJieJian = null
	wanJiaKongZhiDaiLi = null
	ziYouShiJiaoDaiLi = null
	
	constructor() {
		super()
		
		if (sheZhi.kuaiJieJian[this.kuaiJieJianBianHao]) this.kuaiJieJian = sheZhi.kuaiJieJian[this.kuaiJieJianBianHao] // 从存储中加载快捷键
		else this.kuaiJieJian = sheZhi.kuaiJieJian[this.kuaiJieJianBianHao] = moRenKuaiJieJian

		this.wanJiaKongZhiDaiLi = new Proxy(wanJiaKongZhi, {
			get: function (o, p) {
				if (p == 'xieZai' && zhiZaoMoShi.jiaZaiZhuangTai) zhiZaoMoShi.xieZai()
				if (typeof o[p] == 'function') return o[p].bind(o)
				return Reflect.get(...arguments)
			}
		})

		this.ziYouShiJiaoDaiLi = new Proxy(ziYouShiJiao, {
			get: function (o, p) {
				if (p == 'xieZai' && bianJiMoShi.jiaZaiZhuangTai) bianJiMoShi.xieZai()
				if (typeof o[p] == 'function') return o[p].bind(o)
				return Reflect.get(...arguments)
			}
		})
		// 菜单事件
		let kaiQiZiYouShiJiao = this.kaiQiZiYouShiJiao.bind(this)
		let kaiQiBianJiMoShi = this.kaiQiBianJiMoShi.bind(this)
		let kaiQiWanJiaShiJiao = this.kaiQiWanJiaShiJiao.bind(this)
		let kaiQiZhiZaoMoShi = this.kaiQiZhiZaoMoShi.bind(this)

		jieMian.caiDan.bianJiMoShi = () => {
			kaiQiZiYouShiJiao()
			kaiQiBianJiMoShi()
		}
		jieMian.caiDan.zhiZaoMoShi = () => {
			kaiQiWanJiaShiJiao()
			kaiQiZhiZaoMoShi()
		}
	}
	
	chuLi(shiJian) {
		if (shiJian.type == 'keydown') this.keydown(shiJian)
	}
	
	keydown(shiJian) {
		let kjj = this.kuaiJieJian
		
		switch (shiJian.code) {
			case kjj.wanJiaKongZhi.kuaiJieJian:
				this.kaiQiWanJiaShiJiao()
				break;
			case kjj.ziYouShiJiao.kuaiJieJian:
				this.kaiQiZiYouShiJiao()
				break;
			case kjj.zhiZaoMoShi.kuaiJieJian:
				this.kaiQiZhiZaoMoShi()
				break;
			case kjj.bianJiMoShi.kuaiJieJian:
				this.kaiQiBianJiMoShi()
				break;
		}
	}

	kaiQiWanJiaShiJiao() {
		this.ziYouShiJiaoDaiLi.xieZai()
		this.wanJiaKongZhiDaiLi.jiaZai()
	}
	
	kaiQiZiYouShiJiao() {
		this.wanJiaKongZhiDaiLi.xieZai()
		this.ziYouShiJiaoDaiLi.jiaZai()
	}
	
	kaiQiZhiZaoMoShi() {
		if (!wanJiaKongZhi.jiaZaiZhuangTai) this.kaiQiWanJiaShiJiao()
		if (zhiZaoMoShi.jiaZaiZhuangTai) zhiZaoMoShi.xieZai()
		else zhiZaoMoShi.jiaZai()
	}
	
	kaiQiBianJiMoShi() {
		if (!ziYouShiJiao.jiaZaiZhuangTai) this.kaiQiZiYouShiJiao()
		if (bianJiMoShi.jiaZaiZhuangTai) bianJiMoShi.xieZai()
		else bianJiMoShi.jiaZai()
	}
}